#ifndef FLOWMETERSLI1000_H
#define FLOWMETERSLI1000_H

#include "basemodule.h"
#include <QTimer>
#include <QVariant>

class FlowmeterSLI1000 : public BaseModule
{
    Q_OBJECT

#if defined(_WIN32)
typedef quint32(__cdecl *FctOpenPort)(quint8 aPortType, char *aPortConfig, quint32 *aPortHandle);
typedef quint32(__cdecl *FctClosePort)(quint32 aPortHandle);
typedef quint32(__cdecl *FctGetSensorPartName) (quint32 aPortHandle, quint8 aSlaveAdr, char* aPartNameString, quint32 aStringMaxSize);
typedef const char*(__cdecl *FctTranslateErrorCode)(quint32 aErrorCode);
typedef quint32(__cdecl *FctStartContinuousMeasurement)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, quint16 aInterval);
typedef quint32(__cdecl *FctStopContinuousMeasurement)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr);
typedef quint32(__cdecl *FctGetLastMeasurementSigned)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, qint16* aMeasureResult);
typedef quint32(__cdecl *FctGetMeasurementBufferSigned)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, qint16 aMeasureResult[127], quint8* aLength);
#else
typedef quint32(*FctOpenPort)(quint8 aPortType, char *aPortConfig, quint32 *aPortHandle);
typedef quint32(*FctClosePort)(quint32 aPortHandle);
typedef quint32(*FctGetSensorPartName) (quint32 aPortHandle, quint8 aSlaveAdr, char* aPartNameString, quint32 aStringMaxSize);
typedef const char*(*FctTranslateErrorCode)(quint32 aErrorCode);
typedef quint32(*FctStartContinuousMeasurement)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, quint16 aInterval);
typedef quint32(*FctStopContinuousMeasurement)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr);
typedef quint32(*FctGetLastMeasurementSigned)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, qint16* aMeasureResult);
typedef quint32(*FctGetMeasurementBufferSigned)(quint32 aPortHandle, quint8 aBroadcastMode, quint8 aSlaveAdr, qint16 aMeasureResult[127], quint8* aLength);
#endif

public:
    enum FlowmeterSLI1000ConfigID
    {
        ConfigID_Open = 0,
        ConfigID_Close,
        ConfigID_Start,
        ConfigID_Stop,
    };

    explicit FlowmeterSLI1000(QObject *parent = nullptr);
    virtual ~FlowmeterSLI1000() override;

    virtual bool setConfig(int id, QVariant &var) override;
    virtual bool getConfig(int id, QVariant &var) override;

signals:
    void requestSend(const QByteArray &ba);
    void getData(double data);

public slots:
    virtual void onStart() override;
    void onTimeout();

private:
    void importLib();

    QTimer *m_timer = nullptr;

    FctOpenPort OpenPort;
    FctClosePort ClosePort;
    FctGetSensorPartName GetSensorPartName;
    FctTranslateErrorCode TranslateErrorCode;
    FctStartContinuousMeasurement StartContinuousMeasurement;
    FctStopContinuousMeasurement StopContinuousMeasurement;
    FctGetLastMeasurementSigned GetLastMeasurementSigned;
    FctGetMeasurementBufferSigned GetMeasurementBufferSigned;

    quint32 xError;
    quint32 Connection;
};

#endif // FLOWMETERSLI1000_H
